/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
/**
 * 
 */
package org.myontology.view.items.lists;

import static org.myontology.html.Tag.elementById;
import static org.myontology.html.Tag.script;
import static org.myontology.html.Tag.valueById;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.myontology.html.Tag;
import org.myontology.model.Property;
import org.myontology.model.Subject;
import org.myontology.view.items.FormBuilder;
import org.myontology.view.items.SingleEditAreaBuilder;
import org.openrdf.model.Value;

/**
 *  @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 */
public class ListEditBuilder extends SingleEditAreaBuilder {

	/**
	 * @param property
	 * @param builder
	 */
	public ListEditBuilder(Property property, FormBuilder builder) {
		super(property, builder);
		propertyFormElement = Subject.NAMEFORMELEMENT+property.getKey();
	}
	
	String propertyFormElement;
	
	@Override
	protected String displayEditField(String entry) {
		
		
		
		Tag actualContent = new Tag("div");
		actualContent.id(propertyFormElement + "_act");
		
		String addContent;
	
		List<Value> arr = property.getValues();

			for( Iterator<Value> it = arr.iterator();it.hasNext();){
				
				Value v = it.next();
				
				actualContent.content(
					getSelectedElementHTML( v.stringValue(), v.stringValue(), "", propertyFormElement+ "_act", propertyFormElement + "_deletebuttonid" ) + " ");
				
			}

		
			addContent = actualContent.close();
			



			Tag deleteDiv = new Tag("div")
		 	.style("float:left; margin-right: 5px;");
		 
		 Tag deleteButton = new Tag("img");
		 
		 deleteButton.id(propertyFormElement + "_deletebuttonid")
		 .src("skins/default/images/delete_unactive.png")
		 .border("")
		 .style("border:none; cursor:pointer;")
		 .onmouseover("myTip('Remove item(s) from selected ones (first mark!)')")
		 .onClick(script("removeSelectedSpans",propertyFormElement,actualContent.getId())).
		 onClick(script("hideDeleteButtonIfNothingSelected",actualContent.getId(),deleteButton.getId()))
		 .onClick("return false");
		
		 deleteDiv.content(deleteButton);
			
		addContent += deleteDiv.close();
	
		Tag addContainer = new Tag("div");
		addContainer.clazz("addContainer");
		
	 
		
		Tag inputField = new Tag("input")
			.id(propertyFormElement+"_sel")
			.tabindex("")
			.name(propertyFormElement+"_sel")
			.value("")
			.onblur("onkeyup();")
			.onkeyup("document.getElementById('" + propertyFormElement + "_addbutton').disabled=false;")
			.onkeypress("if((event.keyCode==13)){document.getElementById('"+ propertyFormElement +"_addbutton').click();}");
		
		Tag addButton = new Tag("input")
		.style("")
		.disabled(true)
		.type("button")
		.id(propertyFormElement + "_addbutton")
		.value("add")
		.onClick(script("addToSelectedElements",actualContent.getId(),propertyFormElement,valueById(inputField.getId()),valueById(inputField.getId()),valueById(inputField.getId()),deleteButton.getId()))
		.onClick(elementById(inputField.getId())+".focus();")
		.onClick(valueById(inputField.getId())+"=''; this.disabled=true;");

		
		
		addContainer.content(inputField);
		addContainer.content(addButton);
		
		addContainer.content(new Tag("div").clazz("inputhint").content("HelpText"));

		addContainer.content(new Tag("div").clazz("clearer").content("&nbsp;"));

		addContainer.content(new Tag("input").type("hidden").name(propertyFormElement).id(propertyFormElement).value(entry));

		
		
		addContent+=addContainer.close();
		
		return addContent;
	}

	protected String getSelectedElementHTML( String id, String label, String comment, String containerId, String deletebuttonId ){

		comment = StringEscapeUtils.escapeJavaScript( comment );
		label = StringEscapeUtils.escapeHtml( label );
		
		return "<span id=\"" + id + "\" class=\"selectedElement\" onmouseover=\"myTip('"+comment+"');\" onclick=\"toggleBackground(this); hideDeleteButtonIfNothingSelected('"+containerId+"','"+deletebuttonId+"');return false;\">" + label + "</span>"; // WITHOUT "\n" !!!
	}
	
	
	
	
	
}
